﻿using System;
using System.Linq;
using PTop.Code;
using PTop.Domain.Entity.WeChat;
using PTop.Domain.IRepository.WeChat;
using PTop.Repository.WeChat;
using System.Collections.Generic;

namespace PTop.Application.WeChat
{
    /// <summary>
    /// 微信自动回复信息App
    /// </summary>	
    public class WxAutoReplyApp
    {
        private IWxAutoReplyRepository service = new WxAutoReplyRepository();

        public List<WxAutoReplyEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<WxAutoReplyEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_Keyword.Contains(keyword));
                expression = expression.Or(t => t.F_Reply.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }
        public WxAutoReplyEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void Delete(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }

        public void SubmitForm(WxAutoReplyEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                service.Update(entity);
            }
            else
            {
                service.Insert(entity);
            }
        }

        #region For Front

        private static readonly object Lock = 1;

        public Dictionary<string, string> GetAllWxAutoReplyList()
        {
            string cacheKey = "WxAutoReplyEntityList";
            var dic = CacheFactory.Cache().GetCache<Dictionary<string, string>>(cacheKey);
            if (dic == null)
            {
                lock (Lock)
                {
                    dic = CacheFactory.Cache().GetCache<Dictionary<string, string>>(cacheKey);
                    if (dic == null)
                    {
                        var expression = ExtLinq.True<WxAutoReplyEntity>();
                        expression = expression.And(t => t.F_State == 1);
                        var pagination = new Pagination
                        {
                            page = 1,
                            rows = 10000,
                            sidx = "F_CreatorTime asc",
                            sord = "asc"
                        };
                        var list = service.FindList(expression, pagination);
                        LogFactory.GetLogger("TextMessag").Info("list:" + list.Count);
                        dic = new Dictionary<string, string>();
                        foreach (var entity in list)
                        {
                            var keys = entity.F_Keyword.Split(',');
                            var reply = entity.F_Reply;
                            foreach (var key in keys)
                            {
                                dic.Add(key.Trim(), reply);
                            }
                        }
                        CacheFactory.Cache().WriteCache(dic, cacheKey, DateTime.Now.AddMinutes(10));
                    }
                }
            }
            return dic;
        }

        /// <summary>
        /// 获取自动回复内容
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public string GetReplyForKeyword(string keyword)
        {
            var dic = GetAllWxAutoReplyList();
            if (dic != null && dic.ContainsKey(keyword))
            {
                return dic[keyword];
            }
            return string.Empty;
        }

        #endregion

    }
}